package com.binvshe.binvshe.account.forgetpwd.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.binvshe.binvshe.account.utils.RegularUtils;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.frameworkbase.customviews.IconTextArrowEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class ForgetPhoneFragment extends BaseFragment {


    @Bind(R.id.itetPhone)
    IconTextArrowEditText mItetPhone;
    @Bind(R.id.btnNext)
    Button mBtnNext;

    public ForgetPhoneFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        initToolBar();
        return view;
    }

    private void initToolBar() {
        getCustomToolbar().setVisibility(View.VISIBLE);
        getCustomToolbar().setTitle("找回密码");
        getCustomToolbar().enableBackButton().setOnClickListener(new BackBtnListener());
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_forget_phone, null);
    }

    private String getPhone() {
        return mItetPhone.getEditText().trim();
    }

    @OnClick(R.id.btnNext)
    public void onClick() {
        if (TextUtils.isEmpty(getPhone())) {
            FragmentUtils.showWarnDialog("请输入手机号码", getFragmentManager());
            return;
        } else if (!RegularUtils.isMobileSimple(getPhone())) {
            FragmentUtils.showWarnDialog("请输入正确的手机号码", getFragmentManager());
            return;
        }
        MyHttpRequest request = new MyHttpRequest(getContext());
        String urlExists = String.format(UrlCenter.EXISTS, new Object[]{getPhone()});
        request.get(urlExists, null, new DataRequestListener<String>(String.class) {
            @Override
            public void onSuccess(String str) {
                try {
                    JSONObject jsonObject = new JSONObject(str);
                    boolean isExist = jsonObject.optBoolean("value");
                    if (!isExist) {//该号码未被注册
                        FragmentUtils.showWarnDialog("该号码未注册", getFragmentManager());
                    } else {
                        FragmentUtils.addFragment(getFragmentManager(), R.id.forgetContainer, ForgetCaptchaFragment.newInstance(getPhone()));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {

            }
        });
    }

    private class BackBtnListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            getActivity().finish();
        }
    }
}
